<?php

class Order_fee_model extends CI_Model {

    function __construct() {
        parent::__construct();

        $this->tableName = "order_fee"; //
    }

    /*
     * 添加
     */

    public function addData($data) {
        $outArr = array();
        if (!empty($data)) {
            $outArr = $this->db->insert($this->tableName, $data);
            $outArr = $this->db->insert_id();
        }
        return $outArr;
    }

    /*
     * 编辑
     */

    public function updateData($paramArr = array()) {
        $optionArr = array(
            "fieldArr" => array(),
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        if (!empty($fieldArr)) {
            $outArr = $this->db->update($this->tableName, $fieldArr, $whereArr);
        }
        return $outArr;
    }

    /*
     * 删除
     */

    public function delData($paramArr = array()) {
        $optionArr = array(
            "whereArr" => array(),
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        $outArr = array();
        $outArr = $this->db->delete($this->tableName, $whereArr);
        return $outArr;
    }

    /*
     * 得到
     */

    public function getData($where = '', $page = 1, $pageCount = 20, $orderBy = "ORDER BY id DESC", $fields = '*') {

        $result = array();
        $sql = "SELECT " . $fields . " FROM " . $this->tableName . " " . $where . " " . $orderBy;
        if ($pageCount > 0) {
            $sql .= " LIMIT " . (($page - 1) * $pageCount) . ", " . $pageCount;
        }
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    /*
     * 得到条数
     */

    function getDataCount($where = '') {
        $num = 0;
        $sql = "SELECT COUNT(*) AS num FROM " . $this->tableName . " " . $where . " LIMIT 1";
        $query = $this->db->query($sql);
        if ($query) {
            $res = $query->result_array();
            $num = $res[0]["num"];
        }
        return $num;
    }

    public function getDataById($id) {
        $outArr = array();
        $outArr = $this->getData("WHERE id=" . $id, 1, 0, "");
        if (!empty($outArr)) {
            $outArr = $outArr[0];
        }
        return $outArr;
    }

    function type() {
        return [
            1 => ["name" => "收入"],
            2 => ["name" => "支出"],
        ];
    }

    function feeType() {
        return [
            1 => ["name" => "运费"],
            2 => ["name" => "加油"],
            3 => ["name" => "过路费"],
            4 => ["name" => "罚款"],
            5 => ["name" => "停车费"],
            6 => ["name" => "上下车"],
            7 => ["name" => "进出门费"],
            8 => ["name" => "卸货费"],
            9 => ["name" => "小费"],
            10 => ["name" => "高速费"],
            11 => ["name" => "道路文件费"],
            12 => ["name" => "扣款"],
            13 => ["name" => "提成"],
        ];
    }

    function unit() {
        return [
            1 => ["name" => "票"],
            2 => ["name" => "升"],
            3 => ["name" => "件数"],
            4 => ["name" => "吨"],
            5 => ["name" => "方"],
        ];
    }

    public function needField($addParam = []) {
        $param = [
            ["field" => "type_id", "name" => "收支类型", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "order_id", "name" => "订单id", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "fee_type", "name" => "费用类型", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "unit", "name" => "计费方式", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
            ["field" => "unit_fee", "name" => "单价", "is_need" => 0, "type" => "float"],
            ["field" => "num", "name" => "数量", "is_need" => 0, "type" => "float"],
            ["field" => "total_fee", "name" => "总费用", "is_need" => 0, "type" => "float"],
            ["field" => "custom_id", "name" => "供应商", "is_need" => 0, "type" => "int"],
            ["field" => "start_mileage", "name" => "开始公里数", "is_need" => 0, "type" => "float"],
            ["field" => "end_mileage", "name" => "结束公里数", "is_need" => 0, "type" => "float"],
            ["field" => "remark", "name" => "备注", "is_need" => 0, "type" => "string"],
            ["field" => "transport_type", "name" => "运输类型", "is_need" => 0, "type" => "int"],
        ];
        if (!empty($addParam)) {
            $param = array_merge($param, $addParam);
        }
        return $param;
    }

    public function payNeedField($addParam = []) {
        $param = [
            ["field" => "pay_time", "name" => "收支日期", "is_need" => 1, "type" => "date_time", "form_type" => "select"],
            ["field" => "pay_shouzhi_bank_id", "name" => "收支账号", "is_need" => 1, "type" => "z_int", "form_type" => "select"],
        ];
        if (!empty($addParam)) {
            $param = array_merge($param, $addParam);
        }
        return $param;
    }

    /*
     * 运输类型
     */

    function transportType() {
        return [
            1 => ["name" => "空车"],
            2 => ["name" => "轻车"],
            3 => ["name" => "重车"],
        ];
    }

}
